package entity

import (
	"github.com/lab-online/pkg/logger"
	"golang.org/x/crypto/bcrypt"
)

func HashPassword(password string) (string, error) {
	hashedPassword, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
	if err != nil {
		return "", err
	}

	logger.Debug("HashPassword", "hashedPassword", string(hashedPassword))
	return string(hashedPassword), nil
}

func (u *User) HashPassword() error {
	hashedPassword, err := HashPassword(u.Password)
	if err == nil {
		u.Password = hashedPassword
	}
	return err
}
